import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import store from '../store'

import router1 from './router_1.js'
import router2 from './router_2.js'
import router3 from './router_3.js'
import router4 from './router_4.js'
import router5 from './router_5.js'
import router6 from './router_6.js'
import router7 from './router_7.js'
import router8 from './router_8.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Login,
    name: 'Login'
  },
  {
    path: '/main',
    name: 'Main',
    component: () => import('../views/Main.vue'),
    children: [
      {
        path: 'user_students',
        name: 'UserStudent',
        component: () => import('../views/user/Student.vue'),
        meta: {
          title: [
            '学员管理',
          ]
        }
      },
      {
        path: 'user/student/edit',
        name: 'UserStudentEdit',
        component: () => import('../views/user/Edit.vue'),
        meta: {
          title: [
            '学员管理',
            '添加/修改学生'
          ]
        }
      },
      {
        path: 'user/student/detail',
        name: 'UserStudentDetail',
        component: () => import('../views/user/Detail.vue'),
        meta: {
          title: [
            '学员管理',
            '详情'
          ]
        }
      },
      {
        path: 'user_teachers',
        name: 'UserTeacher',
        component: () => import('../views/user/Teacher.vue'),
        meta: {
          title: [
            '讲师管理'
          ]
        }
      },
      {
        path: 'user_assistteacher',
        name: 'user_AssistTeacher',
        component: () => import('../views/user/AssistTeacher.vue')
      },
      {
        path: 'teach/course/manage',
        name: 'TeachCourseManage',
        component: () => import('../views/teach/course/Manage.vue')
      },
      {
        path: 'teach/course/catagory',
        name: 'TeachCourseCatagory',
        component: () => import('../views/teach/course/Catagory.vue')
      },



      ...router1,
      ...router2,
      ...router3,
      ...router4,
      ...router5,
      ...router6,
      ...router7,
      ...router8
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局，每次跳转之前都会调用
router.beforeEach((to, from, next) => {
  // to ，对象，将要去的路由对象
  // from , 对象，从哪去的路由对象
  // next，函数
  //      next()  ：允许通过
  //      next(false)  ：不允许通过，不动，没反应
  //      next({name: 'Login'})   ： 重定向，跳转到指定的地址

  // 1. 判断是否是去登录页面
  if (to.name === 'Login') {
    next()
  } else {
    // 2. 判断当前 vuex 中是否有数据
    if (store.state.user) {
      // 如果有信息就通过
      next()
    } else {
      // 否则跳转到登录页面
      next({ name: 'Login' })
    }
  }
})

export default router
